package net.zjitc.controller;

import com.google.gson.Gson;
import net.zjitc.domain.Category;
import net.zjitc.domain.Product;
import net.zjitc.service.CategoryService;
import net.zjitc.service.ProductService;
import net.zjitc.service.impl.CategoryServiceImpl;
import net.zjitc.service.impl.ProductServiceImpl;
import net.zjitc.utils.URLMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 2 * @Author: 洪瑶
 * 3 * @Date: 2018/11/19 10:41
 * 4
 */
public class IndexController {
    CategoryService cservice = new CategoryServiceImpl();
    ProductService pservice = new ProductServiceImpl();

    @URLMapping(url = "/admin")
    public String admin(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return "/login.jsp";
    }
    @URLMapping(url = "/index")
    public String index(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //一级分类
//        List<Category> clist = cservice.findAll();
//        request.setAttribute("clist",clist);
        //二级分类
        List<Product> hotlist = pservice.findhot();
        request.setAttribute("hotlist",hotlist);
        //三级分类
        List<Product> newlist = pservice.findnew();
        request.setAttribute("newlist",newlist);
        return "/WEB-INF/user/index.jsp";
    }




    @URLMapping(url = "/menuCategory")
    public void menuCategory(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<Category> clist = cservice.findAll();
        String json = new Gson().toJson(clist);
//        response.setStatus(200);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(json);
    }
}
